/**
 * 内容加载扩展类
 * ibio-develop
 * 2009-1-18 17:59
 */
package com.ibio8.components.container {
	import com.ibio8.components.helpers.ObjectScaleHelper;
	import com.ibio8.components.container.BaseLoader;
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	/**
	 * 内容加载扩展。
	 * 
	 * <p>
	 * 加载内容容器的扩展，通常由 Flash IDE 库中的 MC 直接继承即可。继承自 BaseLoader。
	 * 其中缩放图片用到了工具类：ObjectScaleHelper。继承此类的 MC 需要包含以下内容：
	 * </p>
	 * <ul>
	 * <li>mc_bar		MovieClip，显示加载的进度</li>
	 * <li>txt_percent	TextField，显示加载的百分比</li>
	 * </ul>
	 * @see com.ibio8.components.helpers.ObjectScaleHelper	ObjectScaleHelper
	 */
	public class MCContentLoader extends BaseLoader {
		/** @private **/
		protected var m_oldW:Number = 100;
		/** @private **/
		protected var m_oldH:Number = 100;
		/** @private **/
		protected var m_scaleMode:uint = 0;
		/** @private **/
		protected var m_onComplete:Function;
		/** @private **/
		protected var m_completeParams:Array = [];
		/** @private **/
		protected var m_this:Object;
		
		/**
		 * 构造函数
		 */
		public function MCContentLoader() {
			m_this = this;
			m_oldW = this.width;
			m_oldH = this.height;
			if (m_this.mc_bar) {
				m_this.mc_bar.visible = false;
			}
			if (m_this.txt_percent) {
				m_this.txt_percent.visible = false;
			}
		}
		
		/**
		* 加载地址
		* @param	data	要加在资源（包括图片、swf 或二进制流）
		*/
		override public function load(data:Object):void {
			if (m_this.mc_bar) {
				m_this.mc_bar.scaleX = 0;
				m_this.mc_bar.visible = true;
			}
			if (m_this.txt_percent) {
				m_this.txt_percent.text = "0%";
				m_this.txt_percent.visible = true;
			}
			if (data) {
				super.load(data);
			}
		}
		
		/**
		* 加载完成后图片的大小
		* @param w 				宽度
		* @param h 				高度
		* @param scaleContent 	true 为自动等比例缩放加载内容的尺寸到(w*h)
		*/
		public function setSize(w:Number, h:Number, scaleContent:Boolean = true):void {
			m_oldW = w;
			m_oldH = h;
			if (scaleContent) {
				m_scaleMode = 2;
			}else{
				m_scaleMode = 1;
			}
		}
		
		/**
		 * 缩放模式
		 * @param value
		 * 0：不缩放
		 * 1：缩放到当前窗口大小
		 * 2：缩放且保持高宽比
		 */
		public function set maintainAspectMode(value:uint):void {
			m_scaleMode = value;
		}
		
		/**
		 * 加载完毕后的回调函数
		 * @param	value	函数名称
		 * @param	...rest	函数的参数
		 */
		public function onComplete(value:Function, ...rest):void {
			m_onComplete = value;
			m_completeParams = rest;
		}
		
		/** @private **/
		override protected function onLoadedHandler(e:Event):void {
			super.onLoadedHandler(e);
			ObjectScaleHelper.maintainAspect(m_loader, m_oldW, m_oldH, m_scaleMode);
			if (m_this.mc_bar) {
				m_this.mc_bar.visible = false;
			}
			if (m_this.txt_percent) {
				m_this.txt_percent.visible = false;
			}
			if (m_onComplete != null) {
				m_completeParams.splice(0, 0, m_loader.content);
				m_onComplete.apply(null, m_completeParams);
			}
		}
		
		/** @private **/
		override protected function onProgressHandler(e:ProgressEvent):void {
			var percent:uint = Math.round(e.bytesLoaded / e.bytesTotal * 100);
			if (m_this.mc_bar) {
				m_this.mc_bar.scaleX = percent / 100;
			}
			if (m_this.txt_percent) {
				m_this.txt_percent.text = percent + "%";
			}
		}
	}
}

